// pagesEnterprise/pages/housingDetail/housingDetail.js
import ajax from "../../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    housingTypeList: ['写字楼', '商铺', '土地'],
    detailId: null,
    entity: null,
    swiperIndex: 1,
    isEdit: false,
  /**
     * 分享弹出框
     */
    showShare: false
  },
  /**
   * 关注事件
   */
  async followEvent () {
    const {detailId, entity} = this.data
    await ajax.post('/follow/add', {
      assessId: detailId,
      followType: 1,
      state: entity.follow ? 0 : 1
    })
    ajax.showToast(entity.follow ? '已取消关注' : '已关注')
    this.getDetaiRequest()
  },
  /**
   * 显示/隐藏分享框
   */
  shareEvent(event) {
    this.setData({ showShare: !(event.detail == 1) })
  },
  msgEvent () {
    ajax.showToast('开发中...')
  },
  callEvent () {
    const hoseAgent = this.data.entity.hoseAgent
    ajax.makePhoneCall(hoseAgent.phone)
  },
  deleteEvent() {
    wx.showModal({
      title: '提示',
      content: '确定要删除该房源吗?',
      success: async (res) => {
        if (!res.confirm) return
        await ajax.get(`/my/house/assess/garden/remove?id=${this.data.detailId}`)
        await ajax.showSuccess('操作成功')
        wx.navigateBack()
      }
    })
  },
  editEvent() {
    wx.navigateTo({
      url: `/pagesEnterprise/pages/housingInsert/housingInsert?detailId=${this.data.detailId}`,
    })
  },
  swiperCutEvent(event) {
    this.setData({ swiperIndex: parseInt(event.currentTarget.dataset.type) + 1 })
  },
  swiperChange(event) {
    this.setData({ swiperIndex: event.detail.current + 1 })
  },
  async getDetaiRequest() {
    const res = await ajax.get(`/my/house/assess/gardenHousingInfo?id=${this.data.detailId}`)
    if (res.data.houseRent.housingLabel) res.data.houseRent.housingLabel = res.data.houseRent.housingLabel.split(',')
    this.setData({ entity: res.data })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ detailId: options.detailId || null, isEdit: options.edit || false})
    this.getDetaiRequest()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const {housingAssess, houseRent,housingRentImage } = this.data.entity
    const user = wx.getStorageSync('userInfo')
    const indexImage = housingRentImage?.shift()?.image || null
    return {
      title: `${houseRent.housingName} - ${housingAssess.houseArea}㎡`,
      imageUrl: indexImage,
      path: `/pagesEnterprise/pages/housingDetail/housingDetail?detailId=${this.data.detailId}&shareUserId=${user.id}`
    }
  }
})